package com.sdy.healthcode.h5.controller.user;

import com.sdy.common.model.BizException;
import com.sdy.common.model.FileObj;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.healthcode.biz.constants.RedisConstants;
import com.sdy.healthcode.biz.dto.ReportPost;
import com.sdy.healthcode.biz.service.ReportService;
import com.sdy.mvc.controller.BaseController;
import com.sdy.redis.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/supervise")
@Api(tags = {"群众监督"})
public class SuperviseController extends BaseController {
    @Autowired
    private ReportService reportService;
    @Autowired
    private RedisService redisService;
    
    @PostMapping("/report")
    @ApiOperation(value = "举报信息提交")
    public Response report(HttpServletRequest request, @RequestBody ReportPost reportPost) throws BizException {
        Assert.isNull(reportPost, "参数不能为空。");
        if (reportPost.getImgs() == null) {
            reportPost.setImgs(Collections.emptyList());
        }
        reportPost.setUserId(getUserId(request));
        List<String> imgUrls = new ArrayList<>();
        for (String img : reportPost.getImgs()) {
            FileObj fileObj = redisService.get(RedisConstants.FILE_UPLOAD_UUID + img, FileObj.class);
            Assert.isNull(fileObj, "上传的文件已过期，请重新上传。");
            imgUrls.add(fileObj.getPath());
        }
        reportPost.setImgs(imgUrls);
        reportService.report(reportPost);
        return Response.success();
    }
}
